package com.xxx.shetuan3.service.impl;

import com.xxx.shetuan3.exception.ApiException;
import com.xxx.shetuan3.pojo.Notice;
import com.xxx.shetuan3.pojo.Organization;
import com.xxx.shetuan3.pojo.StudentAndOrganization;
import com.xxx.shetuan3.pojo.User;
import com.xxx.shetuan3.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ChiefServiceImpl implements ChiefService {
    @Autowired
    private StudentAndOrganizationService studentAndOrganizationService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private UserService userService;

    @Override
    public List<Organization> findOrganizationByChiefId(Integer chiefId, Integer start, Integer size) {
        List<Organization> organizationList = organizationService.findPageByChiefId(chiefId, start, size);
        return organizationList;
    }

    @Override
    public Organization findOrganizationByOrganizationId(Integer organizationId) {
        Organization organization1 = organizationService.findOrganizationById(organizationId);

        return organization1;
    }

    @Override
    public boolean approveStudentAndOrganization(Integer studentId, Integer organizationId, String state) {
        StudentAndOrganization studentAndOrganization = studentAndOrganizationService.findStudentAndOrganizationByStudentIdAndOrganizationId(studentId, organizationId);
        boolean res = studentAndOrganizationService.updateStudentAndOrganizationStateByStudentAndOrganizationId(state,studentAndOrganization.getOrganizationId());
        return res;
    }

    @Override
    public List<Notice> pageNoticeByOrganizationId(Integer organizationId,Integer start, Integer size) {
        List<Notice> noticeList = noticeService.queryNoticePageByOrganizationId(start, size, organizationId);
        return noticeList;
    }

    @Override
    public Notice findNoticeByNoticeId(Integer noticeId) {
        Notice notice = noticeService.queryNoticeById(noticeId);
        return notice;
    }

    @Override
    public boolean updateNotice(Notice notice) {
        boolean res = noticeService.updateNotice(notice);
        return res;
    }

    @Override
    public boolean deleteNotice(Integer noticeId) {
        boolean res = noticeService.deleteNoticeById(noticeId);
        return res;
    }

    @Override
    public boolean saveNotice(Notice notice) {
        boolean res = noticeService.saveNotice(notice);
        return res;
    }

    @Override
    public List<User> pageUserByOrganizationId(Integer organizationId, Integer start, Integer size) {
        List<StudentAndOrganization> studentAndOrganizationList = studentAndOrganizationService.findStudents(organizationId, start, size);
        List<User> studentList = new ArrayList<>();
        for(StudentAndOrganization studentAndOrganization:studentAndOrganizationList){
            Integer userId = studentAndOrganization.getStudentId();
            User user = userService.findUserByUserId(userId);
            studentList.add(user);
        }
        return studentList;
    }

    @Override
    public boolean deleteStudentAndOrganizationByStudentIdAndStudentId(Integer studentId, Integer organizationId) {
        StudentAndOrganization studentAndOrganization = studentAndOrganizationService.findStudentAndOrganizationByStudentIdAndOrganizationId(studentId, organizationId);
        Integer studentAndOrganizationId = studentAndOrganization.getId();
        boolean res = studentAndOrganizationService.deleteStudentAndOrganizationById(studentAndOrganizationId);
        return res;
    }
}
